﻿using System;
using System.Data.SqlClient;

using Builderator.Core.Components;
using Builderator.Core.Interfaces;

namespace Builderator.Core
{
    public class BuilderatorSettings : Document, IDocument, IDatabase
    {
        public Guid Identifier
        {
            get;
            set;
        }

        [DocumentProperty("Name", typeof(String), false)]
        public string Name
        {
            get;
            set;
        }

        [DocumentProperty("Plugins", typeof(Uri), false)]
        public string Plugins
        {
            get;
            set;
        }

        [DocumentProperty("Servers", typeof(ServerList), false)]
        public ServerList Servers
        {
            get;
            set;
        }

        public BuilderatorSettings()
            : base()
        {
            this.Identifier = Guid.Empty;

            this.Name = Environment.MachineName.ToLower();

            this.Plugins = @"C:\Program Files\Builderator\Plugins";

            this.Servers = new ServerList();
        }

        public SqlCommand Select(DatabaseTable database)
        {
            throw new NotImplementedException();
        }

        public SqlCommand Insert(DatabaseTable database)
        {
            SqlCommand command = new SqlCommand();

            command.CommandText = string.Format("INSERT INTO actions VALUES (@ID, @Name, @Type, @Prev, @Next, @Associated, @Settings)");

            command.Parameters.AddWithValue("@ID", this.Identifier.ToString());

            command.Parameters.AddWithValue("@Name", this.Name);

            command.Parameters.AddWithValue("@Type", Guid.Empty);

            command.Parameters.AddWithValue("@Prev", Guid.Empty);

            command.Parameters.AddWithValue("@Next", Guid.Empty);

            command.Parameters.AddWithValue("@Associated", Guid.Empty);

            command.Parameters.AddWithValue("@Settings", this.Serialize());

            return command;
        }

        public SqlCommand Update(DatabaseTable database)
        {
            SqlCommand command = new SqlCommand();

            command.CommandText = string.Format("UPDATE actions SET settings='{0}' WHERE identifier='{1}'", this.Serialize(), this.Identifier.ToString());

            return command;
        }

        public SqlCommand Delete(DatabaseTable database)
        {
            throw new NotImplementedException();
        }
    }
}